﻿using OpenCvSharp;
namespace FLANN
{
    public class GeometryFeatureModule
    {
         public class GeometryFeature
        {
            public struct Raw
            {
                public static RotatedRect CircumscribedRectangle;
            }

            public double Angle = 0.0f;
            public double Width = 0.0f;
            public double Length = 0.0f;
            public Point2f Center;
        }

         public static GeometryFeature StandardizeRotatedRectangle(RotatedRect rotated_rectangle)
         {
	         //==============================
	         // 注意
	         // 角度的范围为-0到-180，一定包含-0度，https://robopioneers.vercel.app/
	         // 但不确定是否包含-180度
	         //==============================

	         // 标准化参数结果
	         GeometryFeature parameters = new GeometryFeature();
	         //==============================
	         // 填充信息
	         //==============================

	         parameters.Center = rotated_rectangle.Center;
	         GeometryFeature.Raw.CircumscribedRectangle = rotated_rectangle;

	         //==============================
	         // 确定标准化长宽、转角和向量
	         //==============================

	         // Point2f vertices[4];
	         // rotated_rectangle.points(vertices);

	         double angle = rotated_rectangle.Angle >= 0.0f ? rotated_rectangle.Angle : -rotated_rectangle.Angle;

	         if (rotated_rectangle.Size.Width >= rotated_rectangle.Size.Height)
	         {
		         //==============================
		         // 宽大于等于高
		         //==============================

		         parameters.Length = rotated_rectangle.Size.Width;
		         parameters.Width = rotated_rectangle.Size.Height;

		         if (rotated_rectangle.Angle <= 0)
		         {
			         // 角度为逆时针
			         if (angle < 180.0f)
			         {
				         // [0, -180)

				         parameters.Angle = angle;

				         // parameters.Vectors.Direction = vertices[2] - vertices[1];
				         // parameters.Vectors.ClockwiseDiagonal = vertices[3] - rotated_rectangle.center;
				         // parameters.Vectors.AnticlockwiseDiagonal = vertices[2] - rotated_rectangle.center;
			         }
			         else
			         {
				         // 角度等于180 [180, 180]

				         parameters.Angle = angle;

				         // parameters.Vectors.Direction = vertices[1] - vertices[2];
				         // parameters.Vectors.ClockwiseDiagonal = vertices[1] - rotated_rectangle.center;
				         // parameters.Vectors.AnticlockwiseDiagonal = vertices[0] - rotated_rectangle.center;
			         }
		         }
		         else
		         {
			         // (0, 180]

			         parameters.Angle = 180 - angle;

			         // parameters.Vectors.Direction = vertices[1]  - vertices[2];
			         // parameters.Vectors.ClockwiseDiagonal = vertices[1] - rotated_rectangle.center;
			         // parameters.Vectors.AnticlockwiseDiagonal = vertices[0] - rotated_rectangle.center;
		         }
	         }
	         else
	         {
		         //==============================
		         // 高大于宽
		         //==============================

		         parameters.Length = rotated_rectangle.Size.Height;
		         parameters.Width = rotated_rectangle.Size.Width;

		         if (rotated_rectangle.Angle <= 0)
		         {
			         // 角度为逆时针

			         if (angle < 90.0f)
			         {
				         // [0, 90)

				         parameters.Angle = 90.0f + angle;

				         // parameters.Vectors.Direction = vertices[1] - vertices[0];
				         // parameters.Vectors.ClockwiseDiagonal = vertices[2] - rotated_rectangle.center;
				         // parameters.Vectors.AnticlockwiseDiagonal = vertices[1] - rotated_rectangle.center;
			         }
			         else
			         {
				         // [90 ~ 180]

				         parameters.Angle = angle - 90.0f;

				         // parameters.Vectors.Direction = vertices[0] - vertices[1];
				         // parameters.Vectors.ClockwiseDiagonal = vertices[0] - rotated_rectangle.center;
				         // parameters.Vectors.AnticlockwiseDiagonal = vertices[3] - rotated_rectangle.center;
			         }
		         }
		         else
		         {
			         // 角度为顺时针

			         if (rotated_rectangle.Angle <= 90.0f)
			         {
				         parameters.Angle = 90.0f - angle;

				         // parameters.Vectors.Direction = vertices[1] - vertices[0];
				         // parameters.Vectors.ClockwiseDiagonal = vertices[2] - rotated_rectangle.center;
				         // parameters.Vectors.AnticlockwiseDiagonal = vertices[1] - rotated_rectangle.center;
			         }
			         else
			         {
				         parameters.Angle = 270.0f - angle;

				         // parameters.Vectors.Direction = vertices[0] - vertices[1];
				         // parameters.Vectors.ClockwiseDiagonal = vertices[0] - rotated_rectangle.center;
				         // parameters.Vectors.AnticlockwiseDiagonal = vertices[3] - rotated_rectangle.center;
			         }
		         }
	         }

	         return parameters;
         }
    }
}